<?php

namespace app\controllers;

use Yii;
use app\models\User;
use app\models\Job;
use yii\data\ActiveDataProvider;
use app\models\Department;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use app\commands\Service;
use app\models\Workgroup;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends Service
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all User models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => User::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single User model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new User model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new User();
        $model->createtime = date('Y-m-d H:i:s');

        if ($model->load(Yii::$app->request->post())) {
            $password = Yii::$app->request->post('User[password]');
            $model->password = substr(md5($password), 5, 20);
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }

        $job_options = Job::getOptions();
        $department_options = Department::getOptions();
        $workgroup_options = Workgroup::getOptions("status = '显示'");

        return $this->render('create', [
            'model' => $model,
            'job_options' => $job_options,
            'department_options' => $department_options,
            'workgroup_options' => $workgroup_options
        ]);
    }

    /**
     * Updates an existing User model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $password = Yii::$app->request->post('User')['password'];
            if ($model->getOldAttribute('password') != $password) {
                $model->password = substr(md5($password), 5, 20);
            }
            if ($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }

        $job_options = Job::getOptions();
        $department_options = Department::getOptions();
        $workgroup_options = Workgroup::getOptions();


        return $this->render('update', [
            'model' => $model,
            'job_options' => $job_options,
            'department_options' => $department_options,
            'workgroup_options' => $workgroup_options
        ]);
    }

    //修改自己的密码
    public function actionModifymyselfpassword()
    {
        $model = $this->findModel(Yii::$app->user->getId());
        $model->password = substr(md5(Yii::$app->request->get('pwd')), 5, 20);

        if ($model->save()) {
            return json_encode([
                'status' => 'success',
                'msg' => $model->showname . '的密码修改成功'
            ]);
        }

        return '空';
    }

    /**
     * 修改所属科室.
     *
     * @return string
     */
    public function actionSelectdepartment($id)
    {
        $department_name = Department::findOne($id)->name;
        $user = $this->findModel(Yii::$app->user->getId());
        $user->department_id = (int)$id;
        if ($user->save()) {
            return json_encode(['status' => 'success', 'msg' => '科室已换成' . $department_name]);
        }

        return json_encode(['status' => 'failure', 'msg' => '出现未知错误']);
    }

    /**
     * Deletes an existing User model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
